﻿using Ninject;
using System.Windows;
using System.Windows.Controls;
using TotalApplication.AngelSix;
using TotalApplication.Core;
using TotalApplication.services;
using TotalApplication.viewmodels.Video;
using TotalApplication.viewmodels.Video.Controls;

namespace TotalApplication.Mine_Video
{
    ///// <summary>
    ///// TAG_ITEM图标点击附加属性
    ///// </summary>
    //public class BaseProperty : BaseAttachedProperty<BaseProperty, object>
    //{
    //    override public void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) 
    //    {
    //        if (!(sender is FrameworkElement element))
    //            return;

    //        // Do something
    //        RoutedEventHandler? onLoaded = null;
    //        onLoaded = (s, se) =>
    //        {
    //            // Unhook
    //            element.Loaded -= onLoaded;

    //            element.MouseLeftButtonDown += Element_IconClick;
    //        };
    //        element.Loaded += onLoaded;
    //    }

    //    private void Element_IconClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
    //    {
    //        ((TagItemVM)((FrameworkElement)sender).DataContext).IconClick();
    //        e.Handled = true;
    //    }
    //}

    //public class BaseTagItemProperty : BaseAttachedProperty<BaseTagItemProperty, object>
    //{
    //    override public void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    //    {
    //        if (!(sender is FrameworkElement element))
    //            return;

    //        // Do something
    //        RoutedEventHandler? onLoaded = null;
    //        onLoaded = (s, se) =>
    //        {
    //            // Unhook
    //            element.Loaded -= onLoaded;

    //            element.KeyDown += Element_KeyDown;
    //        };
    //        element.Loaded += onLoaded;
    //    }

        

    //}

    //public class ExpandButtonProperty : BaseAttachedProperty<ExpandButtonProperty, object>
    //{
    //    override public void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    //    {
    //        if (!(sender is FrameworkElement element))
    //            return;

    //        // Do something
    //        RoutedEventHandler? onLoaded = null;
    //        onLoaded = (s, se) =>
    //        {
    //            // Unhook
    //            element.Loaded -= onLoaded;

    //            element.MouseLeftButtonDown += Element_Expand;
    //        };
    //        element.Loaded += onLoaded;
    //    }

    //    private void Element_Expand(object sender, System.Windows.Input.MouseButtonEventArgs e)
    //    {
    //        ((TagItemVM)((FrameworkElement)sender).DataContext).Expand();
    //        e.Handled = true;
    //        // IoC.Get<TagsVM>().Expand(((TagItemVM)((FrameworkElement)sender).DataContext));
    //    }
    //}


    public class TagItemBaseProperty : BaseAttachedProperty<TagItemBaseProperty, object>
    {
        override public void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (!(sender is TagItem element))
                return;

            // Do something
            RoutedEventHandler? onLoaded = null;
            onLoaded = (s, se) =>
            {
                // Unhook
                element.Loaded -= onLoaded;

                element.PreviewMouseDown += Element_PreviewMouseDown;

                element.icon.MouseLeftButtonDown += Element_IconClick;

                element.expandButton.MouseLeftButtonDown += Element_Expand;

                element.editText.KeyDown += Element_KeyDown;
            };
            element.Loaded += onLoaded;
        }

        private void Element_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if ( ((ItemsControl)(GetValue((DependencyObject)sender))).DataContext is TagLIstVM vm)
            {
                if (IoC.Get<TagsVM>().Tags != vm)
                {
                    IoC.Get<TagsVM>().Tags = vm;
                }
                IoC.Get<TagsVM>().Tags.ExpandClose();
                e.Handled = true;
            }
        }

        private void Element_IconClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ((TagItemVM)((FrameworkElement)sender).DataContext).IconClick();
            e.Handled = true;
        }

        private void Element_Expand(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ((TagItemVM)((FrameworkElement)sender).DataContext).Expand();
            e.Handled = true;
            // IoC.Get<TagsVM>().Expand(((TagItemVM)((FrameworkElement)sender).DataContext));
        }

        private void Element_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            // 回车键提交编辑内容
            if (e.Key == System.Windows.Input.Key.Enter && ((TagItemVM)((FrameworkElement)sender).DataContext).IsEdit)
            {
                VideoModelContainer.TagsVM.Edit();
                e.Handled = true;
            }
        }
    }
}
